import { Controller, Get, Post, Delete, Param, Query, Body } from '@nestjs/common';
import { LogService } from './log.service';
import { SearchLogDto } from './dto/search-log.dto';
import { CreateLogDto } from './dto/create-log.dto';

@Controller('logs')
export class LogController {
  constructor(private readonly logService: LogService) {}

  /**
   * 查询日志列表
   */
  @Get()
  async findAll(@Query() searchLogDto: SearchLogDto) {
    return await this.logService.findAll(searchLogDto);
  }

  /**
   * 根据ID查询日志详情
   */
  @Get(':id')
  async findOne(@Param('id') id: string) {
    const log = await this.logService.findOne(+id);
    if (!log) {
      return { message: '日志不存在' };
    }
    return log;
  }

  /**
   * 创建日志
   */
  @Post()
  async create(@Body() createLogDto: CreateLogDto) {
    return await this.logService.create(createLogDto);
  }

  /**
   * 删除日志
   */
  @Delete(':id')
  async remove(@Param('id') id: string) {
    await this.logService.remove(+id);
    return { message: '删除成功' };
  }

  /**
   * 批量删除日志
   */
  @Delete()
  async removeMany(@Body() body: { ids: number[] }) {
    await this.logService.removeMany(body.ids);
    return { message: '批量删除成功' };
  }

  /**
   * 清理过期日志
   */
  @Post('clean')
  async cleanOldLogs(@Body() body: { days?: number }) {
    const deletedCount = await this.logService.cleanOldLogs(body.days || 30);
    return { 
      message: `清理成功，删除了 ${deletedCount} 条日志`,
      deletedCount 
    };
  }

  /**
   * 获取日志统计信息
   */
  @Get('statistics/overview')
  async getStatistics() {
    return await this.logService.getStatistics();
  }
}
